package com.sjx.boundarycallback.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.sjx.boundarycallback.R;

/**
 * Created by sjx on 2022/7/8
 * 三减seekbar
 */
public class HealthSeekBar extends View {

    private RectF mRedRectF;
    private RectF mBlueRectF;
    private RectF mGreenRectF;
    private RectF mGrayRectF;
    private Paint mPaint;
    private Path mPath;
    private Float mOrdinate;
    private int mNormal;
    private int mHope;
    private int mLow;

    public HealthSeekBar(Context context) {
        this(context, null);
    }

    public HealthSeekBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HealthSeekBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mRedRectF = new RectF(22, 0, 36, 90);
        mBlueRectF = new RectF(22, 82, 36, 180);
        mGreenRectF = new RectF(22, 180, 36, 260);
        mGrayRectF = new RectF(22, 252, 36, 312);
        mPath = new Path();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(1);
        setTriangleOrdinate(100f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        /*开始绘制*/
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.RED);
        canvas.drawRoundRect(mRedRectF, 8, 8, mPaint);
        mPaint.setColor(Color.BLUE);
        canvas.drawRect(mBlueRectF, mPaint);
        mPaint.setColor(Color.GRAY);
        canvas.drawRoundRect(mGrayRectF, 8, 8, mPaint);
        mPaint.setColor(Color.GREEN);
        canvas.drawRect(mGreenRectF, mPaint);
        drawTriangle(canvas);
        drawText(canvas);
        canvas.restore();
    }

    private void drawTriangle(Canvas canvas) {
        mPaint.setColor(Color.RED);
        mPath.moveTo(0, mOrdinate);
        mPath.lineTo(20, mOrdinate + 15);
        mPath.lineTo(0, mOrdinate + 30);
        mPath.close();
        canvas.drawPath(mPath, mPaint);
    }

    private void drawText(Canvas canvas) {
        float textHeight = Math.abs(mPaint.descent()) + Math.abs(mPaint.ascent());
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.background_d8d8d8));
        mPaint.setTextSize(20);
        canvas.drawText("过量", 54, textHeight * 1.2f, mPaint);
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.background_gray));
        canvas.drawText("标准", 54, 90 + textHeight * 1.2f, mPaint);
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.white));
        canvas.drawText("三减", 54, 180+textHeight * 1.2f, mPaint);
        canvas.drawText("(20-40g)", 54, 180 + textHeight * 2.4f, mPaint);
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.background_d8d8d8));
        canvas.drawText("稍低", 54, 260 + textHeight * 1.2f, mPaint);
    }


    public void setTriangleOrdinate(Float ordinate) {
        mOrdinate = ordinate;
        invalidate();
    }

    public void setColorRange(int normal, int hope, int low) {
        this.mNormal = normal;
        this.mHope = hope;
        this.mLow = low;
        invalidate();
    }

}
